//
//  UIView+SLQuickInit.m
//  SLBlindBox
//
//  Created by 童玉龙 on 2022/4/22.
//

#import "UIView+SLQuickInit.h"

@implementation UIView (SLQuickInit)

+ (UIView *)quickInitFrame:(CGRect)frame bgColor:(UIColor *)backgroundColor {
    UIView *view = [[self alloc] initWithFrame:frame];
    view.backgroundColor = backgroundColor;
    return view;
}

+ (UIView *)quickInitFrame:(CGRect)frame bgColorStr:(NSString *)backgroundColor {
    return [self quickInitFrame:frame bgColor:[UIColor colorWithHexString:backgroundColor]];
}

@end


@implementation UILabel (SLQuickInit)

+ (UILabel *)quickInitFont:(UIFont *)font color:(UIColor *)textColor {
    return [self quickInitFont:font color:textColor text:nil];
}

+ (UILabel *)quickInitFont:(UIFont *)font colorStr:(NSString *)textColor {
    return [self quickInitFont:font color:[UIColor colorWithHexString:textColor] text:nil];
}

+ (UILabel *)quickInitFont:(UIFont *)font colorStr:(NSString *)textColor text:(nullable NSString *)text {
    return [self quickInitFont:font color:[UIColor colorWithHexString:textColor] text:text];
}

+ (UILabel *)quickInitFont:(UIFont *)font color:(UIColor *)textColor text:(NSString *)text {
    UILabel *label = [[self alloc] init];
    label.font = font;
    label.textColor = textColor;
    label.text = text;
    return label;
}

@end

@implementation UIButton (SLQuickInit)


+ (__kindof UIButton *)quickInitImageNamed:(NSString *)imageName {
    return [self quickInitImageNamed:imageName selectedImage:nil];
}

+ (__kindof UIButton *)quickInitImageNamed:(NSString *)imageName selectedImage:(NSString *)selectedImage {
    UIButton *button = [self buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    if (selectedImage) {
        [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
    }
    return button;
}

+ (__kindof UIButton *)quickInitFrame:(CGRect)frame imageNamed:(NSString *)imageName {
    UIButton *button = [self quickInitImageNamed:imageName];
    button.frame = frame;
    return button;
}

+ (__kindof UIButton *)quickInitFont:(UIFont *)font color:(UIColor *)titleColor title:(nullable NSString *)title {
    UIButton *button = [self buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = font;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    return button;
}

+ (__kindof UIButton *)quickInitFont:(UIFont *)font colorStr:(NSString *)titleColor title:(nullable NSString *)title {
    return [self quickInitFont:font color:[UIColor colorWithHexString:titleColor] title:title];
}

- (void)setImageNormal:(UIImage *)image {
    [self setImage:image forState:UIControlStateNormal];
}

- (void)setImageNormalNamed:(NSString *)image {
    [self setImageNormal:[UIImage imageNamed:image]];
}

- (void)setImageStr:(NSString *)imageName forState:(UIControlState)state {
    [self setImage:[UIImage imageNamed:imageName] forState:state];
}

- (void)addTouncdUnInsideTarget:(id)target action:(SEL)action {
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

@end

@implementation UIImageView (SLQuickInit)

+ (UIImageView *)quickInitImageNamed:(NSString *)imageName {
    return [self quickInitFrame:CGRectZero imageNamed:imageName];
}

+ (UIImageView *)quickInitFrame:(CGRect)frame imageNamed:(NSString *)imageName {
    UIImageView *imageView = [[self alloc] initWithImage:[UIImage imageNamed:imageName]];
    imageView.frame = frame;
    return imageView;
}


@end
